<%
'Publisher:WangJinkun.Date:2011-06-01 
'Copyright (C) evil_god@163.com (http://blog.163.com/evil_god).All rights reserved.
'----------FSO文件及目录处理系列函数Start----------

'函数名：ChkFsoSafePass
'作  用：FSO文件操作区域范围，安全检测  fsoDirRange数组存放允许操作的目录列表
'参  数：pathStr
'返回值：true，false  通过，失败
function ChkFsoSafePass(ByVal pathStr)
	ChkFsoSafePass=False
	pathStr=lcase(pathStr)
	pathStr=replace(pathStr,"\","/")
	pathStr=replace(pathStr,"../","")
	pathStr=replace(pathStr,"./","")
	if instr(pathStr,"/")>0 then pathStr=mid(pathStr,1,(instr(pathStr,"/")-1))
	for fsoDirRange_i=0 to ubound(fsoDirRange)
		if pathStr=fsoDirRange(fsoDirRange_i) then
			ChkFsoSafePass=True
			exit for
			exit function
		end if
	next
end function


'函数名：NewFile
'作  用：新建文本型文件
'参  数：fileName  '支持多层目录 如：a/b/c/test.txt 
'返回值：true，false  成功，失败
function NewFile(ByVal fileName)
	if ChkFsoSafePass(fileName)=False then
		NewFile=False
		exit function
	end if
    fileName=replace(fileName,"\","/")
	if InStr(fileName,"/")>0 then NewFolder(Mid(fileName,1,InStrRev(fileName,"/")))
    fileName=Server.MapPath(fileName)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If not objFSO.FileExists(fileName) Then 
		objFSO.CreateTextFile fileName
		NewFile=true
	else
		NewFile=false
	end if
	Set objFSO = Nothing 
end function

'函数名：NewFolder
'作  用：新建文件夹
'参  数：folderName  '支持多层目录 如： a/b/c
'返回值：true，false  成功，失败
function NewFolder(ByVal folderName)
	if ChkFsoSafePass(folderName)=False then
		NewFolder=False
		exit function
	end if
    folderName=replace(folderName,"\","/")
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	if instr(folderName,"/")<=0 then
		If not objFSO.FolderExists(Server.MapPath(folderName)) Then 
			objFSO.CreateFolder Server.MapPath(folderName)
		end if
	else
		arrFolderName=split(folderName,"/")
		tempStr=""
		for arrFolderName_i=0 to ubound(arrFolderName)
			if tempStr="" then
				tempStr=arrFolderName(arrFolderName_i)
			else
				tempStr=tempStr&"/"&arrFolderName(arrFolderName_i)
			end if
			If not objFSO.FolderExists(Server.MapPath(tempStr)) Then 
				objFSO.CreateFolder Server.MapPath(tempStr)
			end if
		next
	end if
	Set objFSO = Nothing
	NewFolder=True
end function

'函数名：CopyFileTo
'作  用：文件复制
'参  数：cName, tName 原文件，目标文件 '支持多层目录 如： a/b/c/test.txt
'返回值：true，false  成功，失败
function CopyFileTo(ByVal cName,ByVal tName)
	if ChkFsoSafePass(cName)=False or ChkFsoSafePass(tName)=False then
		CopyFileTo=False
		exit function
	end if
    tName=replace(tName,"\","/")
	if InStr(tName,"/")>0 then NewFolder(Mid(tName,1,InStrRev(tName,"/")))
    cName=Server.MapPath(cName)
    tName=Server.MapPath(tName)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FileExists(cName) Then 
		objFSO.CopyFile cName,tName
		CopyFileTo=true
	else
		CopyFileTo=false
	end if
	Set objFSO = Nothing 
end function

'函数名：CopyFolderTo
'作  用：文件夹复制
'参  数：cName, tName 原文件夹，目标文件夹 '支持多层目录 如： a/b/c
'返回值：true，false  成功，失败
function CopyFolderTo(ByVal cName,ByVal tName)
	if ChkFsoSafePass(cName)=False or ChkFsoSafePass(tName)=False then
		CopyFolderTo=False
		exit function
	end if
    cName=replace(cName,"\","/")
    tName=replace(tName,"\","/")
	if right(cName,1) = "/" then cName=Mid(cName,1,len(cName)-1)
	if right(tName,1) = "/" then
		if InStr(cName,"/")<=0 then
			tName=tName & cName
		else
			tName=tName & Mid(cName,InStrRev(cName,"/")+1)
		end if
	end if
	if InStr(tName,"/")>0 then NewFolder(Mid(tName,1,InStrRev(tName,"/")))
    cName=Server.MapPath(cName)
    tName=Server.MapPath(tName)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FolderExists(cName) Then 
		objFSO.CopyFolder cName,tName
		CopyFolderTo=true
	else
		CopyFolderTo=false
	end if
	Set objFSO = Nothing 
end function

'函数名：MoveFileTo
'作  用：文件移动
'参  数：cName, tName 原文件，目标文件 '支持多层目录 如： a/b/c/test.txt
'返回值：true，false  成功，失败
function MoveFileTo(ByVal cName,ByVal tName)
	if ChkFsoSafePass(cName)=False or ChkFsoSafePass(tName)=False then
		MoveFileTo=False
		exit function
	end if
    tName=replace(tName,"\","/")
	if InStr(tName,"/")>0 then NewFolder(Mid(tName,1,InStrRev(tName,"/")))
    cName=Server.MapPath(cName)
    tName=Server.MapPath(tName)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FileExists(cName) Then 
		objFSO.MoveFile cName,tName
		MoveFileTo=true
	else
		MoveFileTo=false
	end if
	Set objFSO = Nothing 
end function

'函数名：MoveFolderTo
'作  用：文件夹移动
'参  数：cName, tName 原文件夹，目标文件夹 '支持多层目录 如： a/b/c
'返回值：true，false  成功，失败
function MoveFolderTo(ByVal cName,ByVal tName)
	if ChkFsoSafePass(cName)=False or ChkFsoSafePass(tName)=False then
		MoveFolderTo=False
		exit function
	end if
    cName=replace(cName,"\","/")
    tName=replace(tName,"\","/")
	if right(cName,1) = "/" then cName=Mid(cName,1,len(cName)-1)
	if right(tName,1) = "/" then
		if InStr(cName,"/")<=0 then
			tName=tName & cName
		else
			tName=tName & Mid(cName,InStrRev(cName,"/")+1)
		end if
	end if
	if InStr(tName,"/")>0 then NewFolder(Mid(tName,1,InStrRev(tName,"/")))
    cName=Server.MapPath(cName)
    tName=Server.MapPath(tName)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FolderExists(cName) Then 
		objFSO.MoveFolder cName,tName
		MoveFolderTo=true
	else
		MoveFolderTo=false
	end if
	Set objFSO = Nothing 
end function

'函数名：DelFile
'作  用：删除文件
'参  数：fileUrl  '参数为相对路径 
'返回值：True  ---删除成功
Function DelFile(ByVal fileUrl)
	if ChkFsoSafePass(fileUrl)=False then
		DelFile=False
		exit function
	end if
    fileUrl=Server.MapPath(fileUrl)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FileExists(fileUrl) Then 
		objFSO.DeleteFile fileUrl,True
		DelFile=True
	end If 
	Set objFSO = Nothing 
End Function 

'函数名：DelFolder
'作  用：删除文件夹
'参  数：folderUrl  '参数为相对路径 
'返回值：True  ---删除成功
Function DelFolder(ByVal folderUrl)
	if ChkFsoSafePass(folderUrl)=False then
		DelFolder=False
		exit function
	end if
	folderUrl=Server.MapPath(folderUrl)
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
	If objFSO.FolderExists(folderUrl) Then 
		objFSO.DeleteFolder folderUrl,True
		DelFolder=True
	end If 
	Set objFSO = Nothing 
End Function 

'函数名：ReadFile
'作  用：读取文本文件
'参  数：fileUrl  '参数为相对路径，rType 读取方式，num数字 
'返回值：字符串  ---读取成功
function ReadFile(ByVal fileUrl,rType,num)
	if ChkFsoSafePass(fileUrl)=False then
		ReadFile=False
		exit function
	end if
	if rType="" then rType="ra"
	if num<>"" then 
		num=clng(num)
	else
		num=0
	end if
	if num<=0 then num=1
	Set objFSO=Server.CreateObject("Scripting.FileSystemObject")
		If objFSO.FileExists(Server.MapPath(fileUrl)) Then
			Set f=objFSO.OpenTextFile(Server.MapPath(fileUrl), 1)
				select case lcase(rType)
				case "ra"  'ReadAll
					ReadFile=f.ReadAll
				case "r"  'Read
					ReadFile=f.Read(num)
				case "rl"  'ReadLine
					ReadFile=f.ReadLine
				case "wrl"  'whileReadLine
					tempStr=""
					do while f.AtEndOfStream = false
					tempStr=tempStr & f.ReadLine & chr(10)
					loop
					ReadFile=tempStr
				case "s"  'Skip
					f.Skip(num)
					ReadFile=f.ReadAll
				case "sl"  'SkipLine
					f.SkipLine
					ReadFile=f.ReadAll
				end select
			f.Close
			Set f=Nothing
		else
			ReadFile=False
		end if
	Set objFSO=Nothing
end function

'函数名：WriteFile
'作  用：写文本文件
'参  数：fileUrl  '参数为相对路径，wType 写入方式，forType：c 覆盖内容 a 追加内容 ，content写入内容 
'返回值：True,False  ---成功，失败
function WriteFile(ByVal fileUrl,wType,forType,content)
	if ChkFsoSafePass(fileUrl)=False then
		WriteFile=False
		exit function
	end if
	if wType="" then wType="w"
	if forType="" then forType="c"
	select case lcase(forType)
	case "c"  '覆盖原内容
		forTypeBit=2
	case "a"  '在文件末尾追加内容
		forTypeBit=8
	end select
	Set objFSO=Server.CreateObject("Scripting.FileSystemObject")
		If objFSO.FileExists(Server.MapPath(fileUrl)) Then
			Set f=objFSO.OpenTextFile(Server.MapPath(fileUrl), forTypeBit, True)
				select case lcase(wType)
				case "w"  'write
					f.write(content)
					WriteFile=True
				case "wl"  'writeline
					f.writeline(content)
					WriteFile=True
				end select
			Set f=Nothing
		else
			WriteFile=False
		end if
	Set objFSO=Nothing
end function

'----------FSO文件及目录处理系列函数Start----------
'Publisher:WangJinkun.Date:2011-06-01 
'Copyright (C) evil_god@163.com (http://blog.163.com/evil_god).All rights reserved.
%>